<template>
  <div class="app-container app-container-compactness">
    <!--    <el-button @click="reqApi">发起请求</el-button>-->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px" :label-position="'left'">
<!--      <el-form-item label="项目名称" prop="project_name">-->
<!--        <el-input-->
<!--          v-model="queryParams.project_name"-->
<!--          style="width: 130px;"-->
<!--          class="wenbnen"-->
<!--          placeholder="请输入项目名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="入库编号" prop="warehousing_id">
        <el-input
          v-model="queryParams.warehousing_id"
          style="width: 130px;"
          class="wenbnen"
          placeholder="请输入入库编号"
          clearable
          size="mini"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batch_no">
        <el-input
          v-model="queryParams.batch_no"
          style="width: 130px;"
          class="wenbnen"
          placeholder="请输入批次号"
          clearable
          size="mini"
        />
      </el-form-item>
      <el-form-item label="入库状态" prop="status">
        <el-select v-model="queryParams.status" style="width: 130px;" class="wenbnen" placeholder="入库状态" clearable size="small">
          <el-option label="未入库" value="0" />
          <el-option label="全部入库" value="1" />
          <el-option label="部分入库" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="日期范围" prop="date_select_type">
        <el-radio-group v-model="date_select_type">
          <el-radio :label="'今天'">今天</el-radio>
          <el-radio :label="'近7天'">近7天</el-radio>
          <el-radio :label="'近30天'">近30天</el-radio>
          <el-radio :label="'三个月前'">三个月前</el-radio>
          <el-radio :label="'自定义'">自定义</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        v-show="date_select_type === '自定义'"
        label="创建日期"
      >
        <el-date-picker
          ref="pick1"
          v-model="queryParams.created_at"
          style="transform: translateY(5px); height: 20px; font-size: 12px; line-height: 20px;"
          value-format="yyyy-MM-dd"
          type="daterange"
          size="mini"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row type="flex">
      <el-button v-action="'warehousing@create'" icon="el-icon-plus" type="primary" size="mini" @click="openCreate()">新增
      </el-button>
      <el-button
        v-action="'warehousing@delete'"
        style="margin-left: 0"
        size="mini"
        type="danger"
        icon="el-icon-delete"
        :disabled="multipleSelection.length != 1"
        @click="handleDel2"
      >删除
      </el-button>

    </el-row>
    <el-row type="flex" justify="end">
      <el-popover placement="bottom" width="50" trigger="click">
        <el-checkbox-group v-model="checkedTableColumns">
          <el-checkbox
            v-for="(item, index) in colTableHeaderTit"
            :key="index"
            :label="item.label"
          >{{ item.label }}</el-checkbox>
        </el-checkbox-group>
        <el-button
          slot="reference"
          type="primary"
          style="margin-right: 10px;
          position: relative;
          bottom: 20px;
          "
        >设置显示字段
        </el-button>
      </el-popover>
    </el-row>
    <!-- <el-table ref="table1" v-loading="loading" :row-key="(row) => {return row.id;}" fit border size="mini" :data="list" highlight-current-row @selection-change="handleSelectionChange" @cell-dblclick="dblclick"> -->
    <!-- <el-table-column
        type="selection"
        width="45"
        :selectable="selectable"
      />
      <el-table-column
        v-for="(item, index) in bindTableColumns"
        :key="`col_${index}`"
        class-name="allowDrag"
        align="center"
        show-overflow-tooltip
        :label="item.label"
        :min-width="item.minWidth"
        sortable="custom"
        :prop="item.prop"
      >
        <template v-slot="scope">

          <template v-if="nimabi">
            <el-row v-if="dropCol[index].prop == 'status'">
              <span v-if="scope.row.status === '0'">未入库</span>
              <span v-else-if="scope.row.status === '1'">全部入库</span>
              <span v-else-if="scope.row.status === '2'">部分入库</span>
            </el-row>
            <span v-else>
              {{ scope.row[dropCol[index].prop] }}
            </span>
          </template>
          <template v-if="!nimabi">
            <el-row v-if="item.prop == 'status'">
              <span v-if="scope.row.status === '0'">未入库</span>
              <span v-else-if="scope.row.status === '1'">全部入库</span>
              <span v-else-if="scope.row.status === '2'">部分入库</span>
            </el-row>
            <span v-else>
              {{ scope.row[item.prop] }}
            </span>
          </template>
        </template>
      </el-table-column> -->
    <!-- <el-table-column prop="warehousing_id" label="入库编号" align="center" />

      <el-table-column prop="project_name" label="项目/委托方" align="center" show-overflow-tooltip width="100">
        <template v-slot="scope">
          <span>{{ scope.row.project_name +'/'+ scope.row.project_name_two }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="customer_business_number" label="客户业务单号" align="center" />
      <el-table-column prop="goods_number" label="商品总数量" align="center" />
      <el-table-column prop="actual_number" label="入库数量" align="center" />
      <el-table-column prop="created_at" label="创建时间" align="center" />
      <el-table-column prop="status" label="入库状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status === '0'">未入库</span>
          <span v-else-if="scope.row.status === '1'">全部入库</span>
          <span v-else-if="scope.row.status === '2'">部分入库</span>
        </template>
      </el-table-column>
      <el-table-column prop="pub_name" label="发布人" align="center" /> -->
    <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-button-->
    <!--            v-if="scope.row.status !== '1'"-->
    <!--            v-action="'warehousingSon@index'"-->
    <!--            size="mini"-->
    <!--            type="success"-->
    <!--            @click="goods(scope.row)"-->
    <!--          >添加商品-->
    <!--          </el-button>-->
    <!--          <el-button-->
    <!--            v-if="scope.row.status === '1'"-->
    <!--            v-action="'warehousingSon@index'"-->
    <!--            size="mini"-->
    <!--            type="success"-->
    <!--            @click="goods(scope.row)"-->
    <!--          >详细-->
    <!--          </el-button>-->
    <!--          <el-button-->
    <!--            v-if="scope.row.status !== '1'"-->
    <!--            v-action="'warehousing@update'"-->
    <!--            size="mini"-->
    <!--            type="primary"-->
    <!--            @click="handleEdit(scope.row)"-->
    <!--          >修改-->
    <!--          </el-button>-->
    <!--          <el-button-->
    <!--            v-if="scope.row.status === '0'"-->
    <!--            v-action="'warehousing@delete'"-->
    <!--            size="mini"-->
    <!--            type="danger"-->
    <!--            @click="handleDel(scope.row)"-->
    <!--          >删除-->
    <!--          </el-button>-->

    <!--          <el-button-->
    <!--            v-action="'warehousingSon@print'"-->
    <!--            size="mini"-->
    <!--            type="warning"-->
    <!--            @click="prints(scope.row)"-->
    <!--          >打印-->
    <!--          </el-button>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!-- </el-table> -->
    <el-table
      ref="table1"
      v-loading="loading"
      class="table-wrap"
      :data="tableData"
      row-key="id"
      border
      fit
      header-cell-class-name="header-cell-color"
      @selection-change="handleSelectionChange"
      @cell-dblclick="dblclick"
    >
      <el-table-column
        type="selection"
        width="45"
      />
      <el-table-column
        v-for="(item, index) in bindTableColumns"
        :key="`colTableHeaderTit_${index}`"
        class-name="allowDrag"
        show-overflow-tooltip
        :prop="tableHeaderTit[index].field"
        :label="tableHeaderTit[index].label"
        align="center"
      />
    </el-table>
    <pagination v-show="total>0" small :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增/修改 -->
    <!--    <AddEdit :open.sync="open" :title="title" :form="form" :select-data="selectData" @getList="getList" />-->
    <!-- 打印 -->
    <Print :open-one.sync="openOne" :warehousing_id="warehousing_id" />
  </div>
</template>
<script>
import Sortable from 'sortablejs'
// eslint-disable-next-line no-unused-vars
import { warehousingList, warehousingDel, warehousingPrint } from '@/api/storehouse'
import { parseTime } from '@/utils'

import AddEdit from './AddEdit'
import Print from './Print'
import { newsList, newsListsb } from '@/api/externalGoods'
export default {
  name: 'StorehouseWarehousing2',
  components: {
    Print
  },
  data() {
    return {
      date_select_type: '近30天',
      nimabi: false,
      tableHeaderTit: [
        { label: '入库编号', field: 'warehousing_id', minWidth: 50, isShow: true },
        // { label: '项目/委托方', field: 'project_name', minWidth: 50, isShow: true },
        { label: '批次号', field: 'batch_no', minWidth: 50, isShow: true },
        { label: '商品总数量', field: 'goods_number', minWidth: 50, isShow: true },
        { label: '入库数量', field: 'actual_number', minWidth: 50, isShow: true },
        { label: '创建时间', field: 'created_at', minWidth: 50, isShow: true },
        { label: '入库状态', field: 'statusStr', minWidth: 50, isShow: true },
        { label: '发布人', field: 'pub_name', minWidth: 50, isShow: true }
      ],
      colTableHeaderTit: [
        { label: '入库编号', field: 'warehousing_id', minWidth: 50, isShow: true },
        // { label: '项目/委托方', field: 'project_name', minWidth: 50, isShow: true },
        // { label: '客户业务单号', field: 'customer_business_number', minWidth: 50, isShow: true },
        { label: '批次号', field: 'batch_no', minWidth: 50, isShow: true },
        { label: '商品总数量', field: 'goods_number', minWidth: 50, isShow: true },
        { label: '入库数量', field: 'actual_number', minWidth: 50, isShow: true },
        { label: '创建时间', field: 'created_at', minWidth: 50, isShow: true },
        { label: '入库状态', field: 'statusStr', minWidth: 50, isShow: true },
        { label: '发布人', field: 'pub_name', minWidth: 50, isShow: true }
      ],
      multipleSelection: [],
      // 遮罩层
      loading: true,
      open: false,
      openOne: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      form: {},
      selectData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 20,
        warehousing_id: undefined,
        status: undefined,
        customer_order_type: undefined
      },
      show: false,
      ids: [],
      warehousing_id: '',
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            end.setTime(end.getTime() + 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '三个月前',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 90)
            start.setTime(Date.parse('2000/01/01'))
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  computed: {
    bindTableColumns() { // 表格展示的字段
      const res = this.colTableHeaderTit.filter((v) => {
        return v.isShow == true
      })
      this.tableHeaderTit = JSON.parse(JSON.stringify(res))
      return res
    },
    checkedTableColumns: {
      get() {
        return this.bindTableColumns.map((v) => v.label)
      },
      set(value) {
        this.nimabi = false
        this.colTableHeaderTit.forEach((v) => {
          if (value.includes(v.label)) {
            v.isShow = true
          } else {
            v.isShow = false
          }
        })

        // this.tableHeaderTit.forEach((v) => {
        //   if (value.includes(v.label)) {
        //     v.isShow = true
        //   } else {
        //     v.isShow = false
        //   }
        // })
      }
    }
  },
  watch: {
    'date_select_type': {
      handler(val) {
        switch (val) {
          case '今天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime(), '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近7天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 6, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '近30天':
            var end = new Date()
            var start = new Date()
            start = parseTime(start.getTime() - 3600 * 1000 * 24 * 29, '{y}-{m}-{d}')
            end = parseTime(end.getTime() + 3600 * 1000 * 24, '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          case '三个月前':
            var end = new Date()
            var start = new Date()
            end = parseTime(end.getTime() - 3600 * 1000 * 24 * 89, '{y}-{m}-{d}')
            start = parseTime(Date.parse('2000/01/01'), '{y}-{m}-{d}')
            this.$set(this.queryParams, 'created_at', [start, end])
            break
          // case '自定义':
          //   var end = new Date()
          //   var start = new Date()
          //   start = parseTime(Date.parse('2000/01/01'), '{y}-{m}-{d}')
          //   end = parseTime(end.getTime(), '{y}-{m}-{d}')
          //   this.$set(this.queryParams, 'created_at', [start, end])
          //   break
        }
      },
      deep: true, // 深度监听
      immediate: true // 初次监听即执行
    }
  },
  created() {
    const id = this.$route.query.id
    if (id) {
      this.queryParams.warehousing_id = id
    }
    this.getList()
  },
  mounted() { this.columnDrop() },
  methods: {
    reqApi() {
      newsListsb({ }).then(response => {
        if (response.code === 10000) {
          console.log(response.data)
        }
      })
      newsListsb({ }).then(response => {
        if (response.code === 10000) {
          console.log(response.data)
        }
      })
    },
    // 列拖拽
    columnDrop() {
      var _this = this
      const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
      this.sortable = Sortable.create(wrapperTr, {
        draggable: '.allowDrag', // 允许拖拽元素(el-table-column上设置class-name为允许拖拽)
        animation: 180,
        delay: 0,
        // 之前调用onEnd方法会出现表格DOM不更新以及表头对不上的情况所以更换为onUpdate方法
        // 参考资料 https://www.jianshu.com/p/fd6eb408d8bd
        onUpdate: function(evt) {
          // 修改items数据顺序
          var newIndex = evt.newIndex
          var oldIndex = evt.oldIndex
          const newItem = wrapperTr.children[newIndex]
          const oldItem = wrapperTr.children[oldIndex]

          // 先删除移动的节点
          wrapperTr.removeChild(newItem)
          // 再插入移动的节点到原有节点，还原了移动的操作
          if (newIndex > oldIndex) {
            wrapperTr.insertBefore(newItem, oldItem)
          } else {
            wrapperTr.insertBefore(newItem, oldItem.nextSibling)
          }
          // 更新items数组(index-1是第一列有一个多选列不支持拖拽,否则会有排序错乱的问题)
          var item = _this.tableHeaderTit.splice(oldIndex - 1, 1)
          _this.tableHeaderTit.splice(newIndex - 1, 0, item[0])
        }

      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    dblclick(row, column, cell, event) {
      this.handleEdit(row)
    },
    rowClick(row, column, event) {
      console.log(row, column)
      this.$refs.table1.toggleRowExpansion(row)
    },
    openCreate() {
      this.$router.push({
        path: '/storehouse/warehousing2/AddEdit:' + `_random_${(new Date()).getTime()}`,
        query: { }
      })
    },
    selectable(row, index) {
      if (row.disabled == 'disabled') {
        return false
      } else {
        return true
      }
    },
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      warehousingList(this.queryParams).then(response => {
        if (response.code === 10000) {
          if (response.data.length > 0) {
            response.data.forEach(item => {
              if (item.status == '0') { item['statusStr'] = '未入库' }
              if (item.status == '1') { item['statusStr'] = '全部入库' }
              if (item.status == '2') { item['statusStr'] = '部分入库' }
            })
          }
          that.tableData = response.data
          that.total = response.count
          that.tableData.forEach(item => {
            if (item.status != 0) {
              item.disabled = 'disabled'
            }
          })
          that.loading = false
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true

      this.$router.push({
        path: '/storehouse/warehousing2/AddEdit:' + row.warehousing_id,
        query: { warehousing_id: row.warehousing_id, status: row.status, _form: JSON.stringify(this.form) }
      })

      this.title = '编辑'
    },
    handleDel2() {
      const that = this
      const id = this.multipleSelection[0].id
      that.$confirm('是否删除该数据？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true

        warehousingDel({ id: id.toString() })
          .then(response => {
            that.loading = false
            if (response.code === 10000) {
              that.$notify.success(response.message)
              that.getList()
            } else {
              that.$notify.error(response.message)
            }
          })
          .catch(() => {
            that.loading = false
          })
      }).catch(() => {
      })
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否删除该数据？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        warehousingDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$notify.success(response.message)
            that.getList()
          } else {
            that.$notify.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 20,
        warehousing_id: undefined,
        status: undefined,
        customer_order_type: undefined
      }
      this.handleQuery()
    },
    /** 入库商品 */
    goods(row) {
      this.$router.push({
        path: '/warehousing/goods',
        query: { warehousing_id: row.warehousing_id, status: row.status }
      })
    },
    /** 打印 */
    prints(row) {
      this.openOne = true
      this.warehousing_id = row.warehousing_id
    }
  }
}
</script>
<!--<style lang="less" scoped>-->

<!--.app-container{-->
<!--  padding-top: 0;-->
<!--  font-size: 12px;-->
<!--  /deep/ .el-table&#45;&#45;mini {-->
<!--    .el-table__cell{-->
<!--      padding: 0;-->
<!--    }-->
<!--  }-->
<!--  /deep/  .el-table__cell {-->
<!--    height: 25px !important;-->
<!--  }-->
<!--  /deep/ .el-form{-->
<!--    input{-->
<!--      font-size: 12px;-->
<!--      height: 20px;-->
<!--    }-->
<!--    .el-form-item&#45;&#45;mini.el-form-item{-->
<!--      margin-bottom: 0;-->
<!--    }-->
<!--    .el-form-item&#45;&#45;small.el-form-item{-->
<!--      margin-bottom: 0;-->
<!--    }-->
<!--    .el-form-item__label{-->
<!--      font-size: 12px;-->
<!--    }-->

<!--  }-->
<!--  /deep/ .el-button{-->
<!--    padding: 3px 5px;-->
<!--    overflow: auto;-->
<!--    border-radius: 0;-->
<!--    .app-container{-->
<!--      height: auto;-->
<!--    }-->
<!--    /deep/ div{-->
<!--      font-size: 12px !important;-->
<!--      line-height: 1 !important;-->
<!--    }-->
<!--  }-->
<!--}-->

<!--</style>-->

